<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="消费者">
                <a-input v-model="queryParam.userKeyword" placeholder="" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="使用状态">
                <a-select v-model="queryParam.status" placeholder="请选择">
                  <a-select-option value="1">启用</a-select-option>
                  <a-select-option value="2">禁用</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="(!advanced && 8) || 24" :sm="24">
              <span
                class="table-page-search-submitButtons"
                :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
              >
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => (this.queryParam = {})">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <s-table
        ref="table"
        size="default"
        rowKey="account"
        :columns="columns"
        :data="loadData"
        :alert="false"
        showPagination="auto"
        listName="dataList"
        :scroll="{ x: 'calc(900px + 100%)' }"
        v-if="btnsPurviewData.query.isShow"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
        <div slot="userInfo" slot-scope="text, record">
          <div>
            <span>{{ record.name }}</span
            >&nbsp;&nbsp;<span>{{ record.mobile }}</span>
          </div>
          <div>{{ record.idCard }}</div>
        </div>
        <span slot="status" slot-scope="text">
          <a-badge :status="text | statusTypeFilter" :text="text | statusFilter" />
        </span>

        <span slot="action" slot-scope="text, record">
          <template v-if="btnsPurviewData.editStatus.isShow">
            <a @click="handleChangeStatus(record)">{{ record.status == 1 ? '冻结' : '启用' }}</a>
          </template>
        </span>
      </s-table>
      <create-freeze-form
        ref="createFreezeForm"
        :visible="freezeVisible"
        :loading="confirmLoading"
        :model="mdl"
        @cancel="_cancelFreezeConsumer"
        @ok="_freezeConsumer"
      >
      </create-freeze-form>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { BTNS_PURVIEW_MAP } from '@/store/mutation-types'
import storage from 'store'
import { STable, Ellipsis } from '@/components'
import { getConsumerList, editConsumerStatus } from '@/api/consumerManage'
import CreateFreezeForm from './modules/CreateFreezeForm'
const columns = [
  {
    title: '序号',
    scopedSlots: { customRender: 'serial' },
    align: 'center',
    fixed: 'left',
    width: 80
  },
  {
    title: '消费者信息',
    scopedSlots: { customRender: 'userInfo' },
    align: 'center'
  },
  {
    title: '合同数',
    dataIndex: 'contractNums',
    customRender: (text) => text + ' 份',
    align: 'center'
  },
  {
    title: '合同金额',
    dataIndex: 'contractAmount',
    customRender: (text) => text + '-',
    align: 'center'
  },
  {
    title: '合同已还款次数',
    dataIndex: 'repaymentNums',
    customRender: (text) => text + ' 次',
    align: 'center'
  },
  {
    title: '合同已还款金额',
    dataIndex: 'repaymentAmount',
    customRender: (text) => text + ' 元',
    align: 'center'
  },
  {
    title: '逾期次数',
    dataIndex: 'overdueNums',
    customRender: (text) => text + '次',
    align: 'center'
  },
  {
    title: '冻结原因',
    dataIndex: 'freezingReason',
    customRender: (text) => text ?? '—',
    align: 'center'
  },
  {
    title: '最后登录时间',
    dataIndex: 'lastLoginTime',
    customRender: (text) => text ?? '—',
    align: 'center'
  },
  {
    title: '状态',
    dataIndex: 'status',
    scopedSlots: { customRender: 'status' },
    align: 'center'
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '100px',
    scopedSlots: { customRender: 'action' },
    align: 'center',
    fixed: 'right'
  }
]

const statusMap = {
  1: {
    status: 'processing',
    text: '启用'
  },
  2: {
    status: 'default',
    text: '冻结'
  }
}

export default {
  name: 'ConsumerList',
  components: {
    STable,
    Ellipsis,
    CreateFreezeForm
  },
  data () {
    this.columns = columns
    return {
      btnsPurviewMap: storage.get(BTNS_PURVIEW_MAP),
      btnsPurviewData: {},
      // create model
      visible: false,
      freezeVisible: false,
      confirmLoading: false,
      mdl: null,
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        return getConsumerList(requestParameters)
          .then((res) => {
            return res.result
          })
          .catch((e) => {})
      },
      selectedRowKeys: [],
      selectedRows: []
    }
  },
  filters: {
    statusFilter (type) {
      return statusMap[type].text
    },
    statusTypeFilter (type) {
      return statusMap[type].status
    }
  },
  created () {
    this.btnsPurviewData = this.btnsPurviewMap[this.$route.meta.permission[0]]
  },
  computed: {},
  methods: {
    /**
     * 取消冻结加盟商
     */
    _cancelFreezeConsumer () {
      this.freezeVisible = false
      const form = this.$refs.createFreezeForm.freezeForm
      form.resetFields() // 清理表单数据（可不做）
    },
    /**
     * 冻结加盟商
     */
    _freezeConsumer () {
      const form = this.$refs.createFreezeForm.freezeForm
      this.confirmLoading = true
      form.validateFields((errors, values) => {
        if (!errors) {
          editConsumerStatus({
            freezingReason: values.freezingReason,
            account: values.account,
            status: 2
          })
            .then((res) => {
              this.freezeVisible = false
              this.confirmLoading = false
              this.$refs.table.refresh()
              this.$message.info(`冻结成功`)
            })
            .catch((e) => {
              this.confirmLoading = false
            })
        } else {
          this.confirmLoading = false
        }
      })
    },
    /**
     * 切换消费者状态
     */
    handleChangeStatus (record) {
      const params = { status: record.status === 1 ? 2 : 1, account: record.account }
      if (record.status === 2) {
        editConsumerStatus(params)
          .then((res) => {
            this.$refs.table.refresh()
            this.$message.info(`启用成功`)
          })
          .catch((e) => {})
      } else {
        this.freezeVisible = true
        this.mdl = { account: record.account, freezingReason: '' }
      }
    }
  }
}
</script>
